import $axios from '../services/http';

const state = () => ({
  exposed: '/exposed',
  attack: '/attack',
  saasExposed: '/saas-exposed',
  aip: '/api',
  advancedSearchOpts: {},
  mappingStatus: 0,
  searchData: {},
  levelStatus: [
    {
      type: 0,
      value: '信息',
      tag: 'info',
    },
    {
      type: 1,
      value: '低危',
      tag: 'primary',
    },
    {
      type: 2,
      value: '中危',
      tag: 'warning',
    },
    {
      type: 3,
      value: '高危',
      tag: 'risk',
    },
    {
      type: 4,
      value: '超危',
      tag: 'danger',
    },
  ],
  commonField: [
    'company',
    'tag',
    'is_alive',
    'data_status',
    'exposure_risk',
    'vulnerability',
    'company_only',
    'tag_company',
    'tag_company_only',
  ],
});

const mutations = {
  SET_ADVANCED_SEARCH_OPTS(state, data) {
    state.advancedSearchOpts = data;
  },
  SET_MAPPING_STATUS(state, data) {
    state.mappingStatus = data;
  },

  setSearchData(state, data) {
    state.searchData = data;
  },
};
const actions = {
  async pageCom({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/api/nanjing/com-tree`,
      data,
    );
  },
  async selectedList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}/api/nanjing/list-selected-value`,
      data,
    );
  },
  async calculateRichness({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}/api/nanjing/calculate-richness`,
    );
  },
  // 分页查询租户标签库
  async queryTagList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.saasExposed}/tenant-tag-library/list`,
      data,
      { headers: { showLoading: false } },
    );
  },
  // 设置高级搜索选项信息
  async setAdvancedSearchOpts({ state, commit, rootState }) {
    const { code, data } = await $axios.get(
      `${rootState.routeServiceV2}${state.attack}/high-search/parameter-dict`,
    );

    const opts = code === 0 ? data : {};
    const commonTags = [
      'company',
      'tag',
      'is_alive',
      'data_status',
      'exposure_risk',
      'vulnerability',
      'company_only',
      'tag_company',
      'tag_company_only',
    ];

    for (const key in opts) {
      opts[key].forEach((el) => {
        if (commonTags.includes(el.value)) {
          el.signValue = `${el.value}`;
        } else {
          el.signValue = `${key}.${el.value}`;
        }
      });
    }
    // console.log(opts, 222222)
    commit('SET_ADVANCED_SEARCH_OPTS', opts);
  },
  // 资产误报
  async markAsset({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.attack}/vul/mark-as-incorrect`,
      data,
    );
  },
  // 批量资产误报
  async batchMarkAsset({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/asset/mark-asset-misinformation-batch`,
      data,
      { headers: { showLoading: false } },
    );
  },
  // 获取配置的攻击面类型集合
  async getExposedTypes({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}${state.attack}/get-attack-types`,
    );
  },
  // 获取左侧数据
  async getLeftList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.attack}/aggregation`,
      data,
    );
  },
  // 获取列表数据
  async getList({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.attack}/search`,
      data,
    );
  },
  // 保存高级检索语句
  async addCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/add-high-search-condition`,
      data,
    );
  },
  // 高级检索语句-分页查询
  async getCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/page-search-record`,
      data,
    );
  },
  // 高级检索语句-删除
  async deleteCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/delete-search-condition`,
      data,
    );
  },
  // 高级检索语句-更新
  async editCondition({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.exposed}/high-search/edit-search-condition`,
      data,
    );
  },
  // 启动测绘
  async startMapping({ state, rootState }, data) {
    return await $axios.post(
      `${rootState.keywordService}/plotting/start`,
      data,
    );
  },
  // /////////////////////////////////////api
  // 新增资产标签
  async appendTagsV2({ commit, state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.aip}/tagging`,
      data,
    );
  },
  // 清空资产标签
  async emptyTags({ commit, state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.aip}/remove-all-tag`,
      data,
    );
  },
  // 删除资产标签
  async deleteTags({ commit, state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.aip}/delete-tag`,
      data,
    );
  },

  // 控股公司关系-查询下一级公司
  async getTree({ commit, state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.aip}/enterprise-relation/get-target-company-tree-paths`,
      data,
    );
  },
  // 分页获取企业名称-下拉框列表
  async getEnterprise({ commit, state, rootState }, data) {
    return await $axios.post(
      `${rootState.routeServiceV2}${state.aip}/enterprise/list-enterprise-for-selected`,
      data,
    );
  },
  ////////////////////////////////
  // 根据id获取漏洞详情
  async getAssetVulById({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}${state.attack}/vul/${data}`,
    );
  },
  // 根据id获取资产列表
  async getAssetList({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}${state.attack}/affect-assets/${data}`,
    );
  },
  // 详情页左侧信息
  async getBaseInfo({ state, rootState }, data) {
    return await $axios.get(
      `${rootState.routeServiceV2}${state.exposed}/asset/asset-base-info`,
      data,
    );
  },
};
export default {
  namespaced: true, // 有命名空间
  state,
  mutations,
  actions,
};

